package com.stylefeng.guns.rest.common.constant;

import com.stylefeng.guns.rest.pay.PayPropertySupport;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class WzPropertySupport {
    private static final String CONFIG_FILE = "wz.properties";
    protected static Log logger = LogFactory.getLog(WzPropertySupport.class);
    protected static Properties p = new Properties();
    static {
        init(CONFIG_FILE);
    }

    protected static void init(String propertyFileName) {
        InputStream in = null;
        try {
            in = PayPropertySupport.class.getClassLoader().getResourceAsStream(propertyFileName);
            if (in != null) {
                p.load(in);
                if (logger.isInfoEnabled()) {
                    logger.info("外赚配置文件加载成功~");
                }
            }
        } catch (IOException e) {
            logger.error("装载 [" + propertyFileName + "] 异常:" + e.getMessage(), e);
        } finally {
            if (in != null)
                try {
                    in.close();
                } catch (IOException e) {
                    in = null;
                    logger.error("释放 [" + propertyFileName + "] 异常:" + e.getMessage(), e);
                }
        }
    }

    public static String getProperty(String key, String defaultValue) {
        return StringUtils.defaultIfEmpty(p.getProperty(key), defaultValue);
    }
    public static String getProperty(String key) {
        return p.getProperty(key);
    }

    public static void reload() {
        reload(CONFIG_FILE);
    }

    public static void reload(String filename) {
        if (p != null) {
            p.clear();
        }
        init(filename);
    }
}
